//
//  OnlineEducationHeaderView.m
//  GC_OC
//
//  Created by 曾 on 2022/1/19.
//

#import "OnlineEducationHeaderView.h"
#import "OnlineHeaderCollectionViewCell.h"
@interface OnlineEducationHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong)NSDictionary* dic;
@property (nonatomic, weak)UIImageView* upDonwImg;
@property (nonatomic, weak)UILabel*     selectLabel;
@property (nonatomic, strong)NSArray*   mediaArr;//左上角菜单数据
@property (nonatomic, strong)NSMutableArray* widthArr;
@property (nonatomic, strong)UICollectionView* collectionView;
@end

@implementation OnlineEducationHeaderView

-(instancetype)initDic:(NSDictionary*)dic{
    self=[super init];
    if(self){
        self.dic = dic;
        self.mediaArr = self.dic[@"media_class"] ;
        [self initView];
    }
    return self;
}
-(void)initView{
    
    CGFloat sdViewheight=1;
    self.sdView = [[SdScrollView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 0)];
    [self addSubview:self.sdView];
    [self.sdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
    }];
    self.sdView.scroDidBlock = ^(NSDictionary * _Nonnull dict) {
        [[KFTools getCurrentVC].view showWarning:@"待跳转" onHide:nil];
    };

    CGFloat collViewheight=1;
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator=NO;
    _collectionView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.0f];
    [_collectionView registerNib:[UINib nibWithNibName:@"OnlineHeaderCollectionViewCell" bundle: nil] forCellWithReuseIdentifier:@"ITEM"];
    [self addSubview:_collectionView];
    [self.collectionView reloadData];
    [self.collectionView layoutIfNeeded];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.sdView.mas_bottom).offset(10);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    self.frame=CGRectMake(0,0,SCREEN_WIDTH,sdViewheight + collViewheight + 10);
}
- (void)setDataSource:(NSArray *)dataSource{
    NSArray *enumArray = [[dataSource reverseObjectEnumerator] allObjects];
    _dataSource = enumArray;
}
- (void)setSdScoImageArray:(NSArray *)sdScoImageArray{
    _sdScoImageArray = sdScoImageArray;
    [self.sdView configData:sdScoImageArray];
}
- (void)reloadHeadView{
    CGFloat height=hig(75);
    
    //这个没用懒得删除了
    self.widthArr=[NSMutableArray new];
    for (int i=0;i<self.dataSource.count;i++){
       CGFloat width=(SCREEN_WIDTH - wid(40))/5;
        [self.widthArr addObject:[NSString stringWithFormat:@"%lf",width]];
    }
    
    //计算滚动图
    CGFloat sdViewheight=1;
    NSInteger sdScroInt = self.sdScoImageArray.count;
    if (sdScroInt == 0) {
        sdViewheight = 0;
    }
    else{
        sdViewheight = 140;
    }
    [self.sdView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(sdViewheight);
    }];
    
    //计算coll视图
    CGFloat collH=1;
    [self.collectionView reloadData];
    [self.collectionView layoutIfNeeded];
    collH = self.collectionView.contentSize.height;
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(collH);
    }];
    self.collectionView.frame=CGRectMake(0,height,SCREEN_WIDTH,collH);
    self.frame=CGRectMake(0,0,SCREEN_WIDTH,sdViewheight + collH + 10);
}
- (void)setCurrenClassID:(NSString *)currenClassID{
    _currenClassID = currenClassID;
    [self.collectionView reloadData];
}
- (void)popMenuButton:(UIButton *)button{
    
}

#pragma -collectionview make
//整体
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(wid(10),wid(20),wid(10),wid(20));//分别为上、左、下、右
}
//大小
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath{
    
    return CGSizeMake((SCREEN_WIDTH - wid(40))/5,hig(85));
}
//列
-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return wid(0);
}
//行
-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return wid(20);
}
//wid(190)  wid(135)
#pragma mark - tableview delegate and datasource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OnlineHeaderCollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ITEM" forIndexPath:indexPath];
    NSDictionary *dic = self.dataSource[indexPath.row];
    [cell initView:[self.widthArr[indexPath.row] floatValue] andDic:dic];
    cell.nameLabel.textColor = UIColor.blackColor;
    cell.nameLabel.font=[UIFont systemFontOfSize:FontSize(10)];
    return cell;
}
//进行点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataSource[indexPath.row];
    if (self.collDidBlock) {
        self.collDidBlock(dic);
    }
}

#pragma mark --lazy
- (TearchFengCaiView *)teacherFCView {
    if (!_teacherFCView) {
        _teacherFCView = [[TearchFengCaiView alloc]initWithFrame:CGRectZero];
    }
    return _teacherFCView;
}

@end
